package com.ruoyi.homeowners.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.homeowners.domain.Homeowners;
import com.ruoyi.homeowners.service.IHomeownersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 户主管理Controller
 * 
 * @author ruoyi
 * @date 2024-10-11
 */
@RestController
@RequestMapping("/homeowners/homeowners")
public class HomeownersController extends BaseController
{
    @Autowired
    private IHomeownersService homeownersService;

    /**
     * 查询户主管理列表
     */
    @PreAuthorize("@ss.hasPermi('homeowners:homeowners:list')")
    @GetMapping("/list")
    public TableDataInfo list(Homeowners homeowners)
    {
        startPage();
        List<Homeowners> list = homeownersService.selectHomeownersList(homeowners);
        return getDataTable(list);
    }

    /**
     * 导出户主管理列表
     */
    @PreAuthorize("@ss.hasPermi('homeowners:homeowners:export')")
    @Log(title = "户主管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Homeowners homeowners)
    {
        List<Homeowners> list = homeownersService.selectHomeownersList(homeowners);
        ExcelUtil<Homeowners> util = new ExcelUtil<Homeowners>(Homeowners.class);
        util.exportExcel(response, list, "户主管理数据");
    }

    /**
     * 获取户主管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('homeowners:homeowners:query')")
    @GetMapping(value = "/{homeownerId}")
    public AjaxResult getInfo(@PathVariable("homeownerId") Long homeownerId)
    {
        return success(homeownersService.selectHomeownersByHomeownerId(homeownerId));
    }

    /**
     * 新增户主管理
     */
    @PreAuthorize("@ss.hasPermi('homeowners:homeowners:add')")
    @Log(title = "户主管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Homeowners homeowners)
    {
        return toAjax(homeownersService.insertHomeowners(homeowners));
    }

    /**
     * 修改户主管理
     */
    @PreAuthorize("@ss.hasPermi('homeowners:homeowners:edit')")
    @Log(title = "户主管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Homeowners homeowners)
    {
        return toAjax(homeownersService.updateHomeowners(homeowners));
    }

    /**
     * 删除户主管理
     */
    @PreAuthorize("@ss.hasPermi('homeowners:homeowners:remove')")
    @Log(title = "户主管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{homeownerIds}")
    public AjaxResult remove(@PathVariable Long[] homeownerIds)
    {
        return toAjax(homeownersService.deleteHomeownersByHomeownerIds(homeownerIds));
    }
}
